package main

import (
	"fmt"
	"time"

	"github.com/sirupsen/logrus"
	"github.com/topfreegames/pitaya/v2/client"
	"github.com/topfreegames/pitaya/v2/examples/demo/cluster_protobuf/protos"
	"github.com/topfreegames/pitaya/v2/serialize/protobuf"
)

func main() {

	c := client.New(logrus.InfoLevel, 100*time.Millisecond)

	err := c.ConnectTo(":3250")
	if err != nil {
		fmt.Println(" conn server error :", err)
		return
	}

	go func(c client.Client) {
		for {
			select {
			case data := <-c.MsgChannel():
				if data.Err {
					fmt.Println("error :", string(data.Data))
					break
				}

				fmt.Printf("data: %#v\n", data)

				var res protos.Response
				protobuf.NewSerializer().Unmarshal(data.Data, res)
				fmt.Printf("res: %#v\n", res)
			}
		}

	}(*c)

	_, err = c.SendRequest("room.room.entry", []byte(""))
	if err != nil {
		fmt.Println("send request error")
		return
	}
	_, err = c.SendRequest("room.room.join", []byte(""))
	if err != nil {
		fmt.Println("send request error")
		return
	}
	select {}
}
